
$.init();
var app=new App();
app.init();
$('#tab2').click(function(){

})
function App(){
  var cart={};
  var foods={};
  var priceTotal=0;
  var init=function(){
    $.getJSON('http://dade.com/index.php/Home/Foods/getFoodsList',function(data){
      foods=data;
      if(getCart()!=null)//获取数据
        cart=getCart();
      bindEvent();
      update();
      $('.loading').hide();
    });
  };
  //绑定事件
  var oneClick=function(el,cb){
    el.unbind('click').click(cb);
  }
  var bindEvent=function(){
    oneClick($('.clear-cart'),clearCart);
    oneClick($('.selected'),toggleSelected);
    oneClick($('.add'),addToCart);
    oneClick($('.minus'),removeFromCart);
    oneClick($('.bg'),toggleSelected);
    if($.isEmptyObject(cart))
      $('.selected').unbind('click');
  }
  //更新数据+视图
  var update=function(){
    saveCart();
    var sum=0;
    $('.minus').hide();
    $('.num').hide();
    priceTotal=0;
    $('#selected li').remove();
    for (var i in cart) {
      var str=/\d+/;
      var id=str.exec(i)[0];
      var el='.cart-'+str.exec(i)[0];
      $(el+' .num').html(cart[i]);
      sum+=cart[i];
      $(el+' .minus').show();
      $(el+' .num').show();
      str='<li class="cart-'+i+' item-content"><div class="item-inner">'+
          '<div class="item-title">'+foods[id].name+'</div>'+
            '<div class="item-after"><span class="li-price">'+parseInt(foods[id].price)*parseInt(cart[i])+'￥</span><a href="#" class=" btn"  ><span id="'+id+'" class="minus iconfont">&#xe608;</span><span class="num">'+cart[i]+'</span><span id="'+id+'" class=" add iconfont">&#xe601;</span></a></div></div></li>';
      $('#selected ul').append(str);
      priceTotal+=parseInt(foods[id].price)*parseInt(cart[i]);
    };
    bindEvent();
    $('.cart-bar .sum').html(sum);
    $('.priceTotal').html(priceTotal+'￥');
    if(!$.isEmptyObject(cart)){
      $('.cart-bar .pay-cart').removeClass('null-cart');
      $('.cart-bar .pay-cart').html('选好了').attr('href','http://dade.com/index.php/Home/Order/create');
    }else{
      $('.cart-bar .pay-cart').addClass('null-cart');
      $('.cart-bar .pay-cart').html('快选餐吧').removeAttr('href');
    }

  };
  //初始化pay视图
  var initPay=function(){
    $('.sel-foods li').remove();
    for (var i in cart) {
      var str=/\d+/;
      var id=str.exec(i)[0];
      str='<li class="cart-'+i+' item-content"><div class="item-inner">'+
          '<div class="item-title">'+foods[id].name+'</div>'+
            '<div class="item-after"><span>x'+cart[i]+'</span><span>'+parseInt(foods[id].price)*parseInt(cart[i])+'￥</span>';
      $('.sel-foods').append(str);
    }
    $('.priceTotal').html(priceTotal+'￥');
  }
  //弹出已选
  var  toggleSelected=function(){
    $('.selected-bottom').toggle();
    $('.price-bottom').toggle();
    $('#selected').toggle();
    $('.bg').toggle();
  }
  // 清空购物车
  var clearCart=function(){
    cart={};
    toggleSelected();
    update();
  }
  //添加到购物车
  var removeFromCart=function(){
      minus=$('.minus');
      id=$(this).attr('id');
      cart['cart'+id]-=1;
      if(cart['cart'+id]==0){
        delete cart['cart'+id];
      }
      update();
  }
  var addToCart=function(){
      var id=$(this).attr('id');
      if(cart['cart'+id]==null)
        cart['cart'+id]=1;
      else cart['cart'+id]+=1;
      update();
  }


  var saveCart=function(){
    localStorage.setItem('cart',JSON.stringify(cart));
  };
  var getCart=function(){
    return JSON.parse(localStorage.getItem('cart'));
  };
  return {
    init:init,
    initPay:initPay,
    bindEvent:bindEvent,
    getCart:getCart,
    clearCart:clearCart
  }
}
